﻿using SvgApplication.Data.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SvgApplication.Authorization
{
    public class RoleAuthorizationAttribute : AuthorizeAttribute
    {
        public UserRoles Modules { get; set; }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {

            bool isAuth = false;
            try
            {
                //get the current user
                var principal = httpContext.User as AuthenticationPrincipal;
                if (principal != null)
                {
                    if (principal.UserData != null)
                    {
                        //get the user permissions
                        var userAvailableModules = WebUserAccess.GetUserAccess(principal.UserData.UserID);
                        if (Modules.HasFlag(userAvailableModules))
                        {
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
               // Logger.Log.ErrorToDb(ex);
            }
            return isAuth;

        }

    }
   
}